package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.ParkDao;
import cn.topevery.garden.entity.po.Park;
import cn.topevery.garden.entity.query.ParkQuery;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.DbScriptHelper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author code-generate
 * @date 2021-08-06
 */
@Repository
public class ParkDaoImpl extends BaseDaoImpl<Park> implements ParkDao {
    @Override
    public <P extends DbCsp> List<Park> getSearch(P para) {
        return null;
    }


    @Override
    public DataPager<Park> listPage(ParkQuery query) {
        String table = "t_park tp left join t_park_class tc on tc.c_id = tp.c_class_small_id " +
                "left join t_sys_dept tsd on tsd.c_dept_id=tp.c_mgr_dept_id ";

        String columns = "tp.c_id,tp.c_code,tp.c_name,tc.c_name c_class_big_name,tp.c_measure,tp.c_address,tsd.c_dept_name mgrDeptName,tp.c_geom_wgs";
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tp.c_db_create_date desc");
        sqlQueryMate.
                where(true, "tp.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getName()),"tp.c_name",ScriptConditionEnum.Like,query.getName())
                .where(StringUtils.isNotBlank(query.getCode()),"tp.c_code",ScriptConditionEnum.Like,query.getCode())
                .where(query.getSectionId()!=null,"tp.c_section_id",ScriptConditionEnum.Eq,query.getSectionId())
                .where(query.getType()!=null,"tp.c_class_small_id",ScriptConditionEnum.Eq,query.getType())
                .where(StringUtils.isNotBlank(query.getClassBigName()),"tp.c_class_big_name",ScriptConditionEnum.Like,query.getClassBigName())
                .where(StringUtils.isNotBlank(query.getDistractId()), "tp.c_distract_id", ScriptConditionEnum.Eq, query.getDistractId())
                .where(StringUtils.isNotBlank(query.getStreetId()), "tp.c_street_id", ScriptConditionEnum.Eq, query.getStreetId())
                .where(StringUtils.isNotBlank(query.getCommunityId()), "tp.c_community_id", ScriptConditionEnum.Eq, query.getCommunityId())
        ;
        return getDataPager(sqlQueryMate, pageSize, pageIndex, Park.class);
    }

    @Override
    public List<Park> getListBySectionId(List<Integer> ids) {
        String table = "t_park tp";

        String columns = "c_id,c_code,c_name,c_class_big_name,c_measure";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tp.c_db_create_date desc");
        sqlQueryMate.
                where(true, "tp.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tp.c_id", ScriptConditionEnum.In, ids)
        ;

        return selectList(sqlQueryMate,  Park.class);
    }

    @Override
    public List<Map<String, Object>> getShortList() {
        SqlQueryMate sqlQueryMate = DbScriptHelper.getSelectSqlMate("t_park tp")
                .setColumns("c_id as id, c_name as name");
        sqlQueryMate
                .where(true, "tp.c_db_status", ScriptConditionEnum.Eq, 0);
        ;
        return getMap(sqlQueryMate);
    }

    /**
     * 描述 通过编码和名称获取公园Id
     *
     * @param code the code
     * @param name the name
     * @return the park id by code and name
     * @author wenzhao.lin
     * @created 2021 -08-19 14:44:45
     */
    @Override
    public Integer getParkIdByCodeAndName(String code, String name) {
        String table = "t_park tp";
        String columns = "tp.*";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tp.c_code", ScriptConditionEnum.Eq, code)
                .where(StringUtils.isNotEmpty(name), "tp.c_name", ScriptConditionEnum.Eq, name)
        ;
        Integer tempId = null;
        List<Park> list = selectList(sqlQueryMate, Park.class);
        if(list != null && list.size() > 0){
            tempId = list.get(0).getId();
        }
        return tempId;
    }

}
